package TestPojoNetty;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.example.discardMine.TimeClientHandler;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class PojoClient {
	public static void run() {
		// TODO Auto-generated method stub
		ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
		ClientBootstrap bootstrap = new ClientBootstrap(factory);
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {			
			@Override
			public ChannelPipeline getPipeline() throws Exception {
				// TODO Auto-generated method stub
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("encoding", new EncodingHandle());
				pipeline.addLast("decoding", new DecodingHandle());
				pipeline.addLast("handle", new ClientHandle());			
				return pipeline;
			}
		});
		
		bootstrap.setOption("tcpNoDelay", true);
		bootstrap.setOption("keepAlive", true);		    
		bootstrap.connect(new InetSocketAddress("localhost", 8080));
		
		/*String host = "localhost";
		int port = 8080;
		
		ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
		ClientBootstrap bootstrap = new ClientBootstrap(factory);
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			@Override
			public ChannelPipeline getPipeline() throws Exception {
				// TODO Auto-generated method stub				
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("encoding", new StringEncoder());
				pipeline.addLast("decoding", new StringDecoder());
				pipeline.addLast("a", new TimeClientHandler());			
				return pipeline;
			}
		});
		bootstrap.setOption("tcpNoDelay", true);
		bootstrap.setOption("keepAlive", true);		    
		bootstrap.connect(new InetSocketAddress(host, port));*/
	}
	public static void main(String[] args) {
		run();
	}
}
